<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.username" clearable class="filter-item" style="width: 200px;" placeholder="请输入用户名"/>

      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
<!--      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>-->
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" width="100px" label="用户ID" prop="id" sortable/>
      <el-table-column align="center" label="姓氏" prop="csurname"/>
      <el-table-column align="center" label="用户名" prop="userName"/>

      <el-table-column align="center" label="申请人ID" prop="applyUserId"/>

<!--      <el-table-column align="center" label="性别" prop="gender">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tag >{{ genderDic[scope.row.gender] }}</el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <!-- <el-table-column align="center" label="生日" prop="birthday"/> -->

      <!-- <el-table-column align="center" label="用户等级" prop="userLevel">
        <template slot-scope="scope">
          <el-tag >{{ levelDic[scope.row.userLevel] }}</el-tag>
        </template>
      </el-table-column> -->

      <el-table-column align="center" label="申请时间" prop="applyTime" sortable/>

      <el-table-column align="center" label="原始职位编码" prop="originalPositionCode" sortable/>

      <el-table-column align="center" label="原始职位名称" prop="originalPositionName" sortable/>


        <el-table-column align="center" label="申请职位编码" prop="applyPositionCode" sortable/>

        <el-table-column align="center" label="申请职位名称" prop="applyPositionName" sortable/>
        <!-- <el-table-column align="center" label="PV值" prop="pvValue" sortable/> -->

      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag>{{ statusDic[scope.row.ustatus] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.ustatus == '0'" type="primary" size="mini" @click="handleDetail(scope.row,false)">审核</el-button>
          <el-button  type="primary" size="mini" @click="handleDetail(scope.row,true)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <!-- 用户编辑对话框 -->
    <el-dialog :visible.sync="userDialogVisible" title="用户审核">
      <el-form ref="userDetail" :model="userDetail" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="身份证">
          <img :src="userDetail.idCardFile" alt=""  width="100%" >
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="中文姓">
              <span>{{userDetail.csurname}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中文名">
              <span>{{userDetail.cname}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="英文姓">
              <span>{{userDetail.esurname}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文名">
              <span>{{userDetail.ename}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="身份证号码">
          <span>{{userDetail.idCard}}</span>
        </el-form-item>
        <!-- <el-form-item label="收款人姓名">
          <span>{{userDetail.name}}</span>
        </el-form-item> -->
        <el-form-item label="银行卡">
          <span>{{userDetail.phone}}</span>
        </el-form-item>
        <el-form-item label="开户行">
          <span>{{userDetail.bankCard}}</span>
        </el-form-item>
        <el-form-item label="入会表格">
          <el-button size="mini" @click="downTable(userDetail.tableFile)">下载表格</el-button>
        </el-form-item>
        <el-form-item label="审核状态" prop="userLevel">
          <el-select v-model="uStatus" placeholder="请选择"><el-option v-for="(item, index) in statusDic" :key="index" :label="item" :value="index" /></el-select>
        </el-form-item>
        <el-form-item label="备注" prop="reviewDesc">
          <el-input v-model="reviewDesc" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="userDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleUserUpdate">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList ,userUpPosition ,updateUser } from '@/api/pv'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'pv',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      isdisabled:false,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        username: undefined,
        mobile: undefined,
        userId: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      reviewDesc:'',
      uStatus:'',
      id:'',
      userDetail:{},
      downloadLoading: false,
      //statusDic: ['未知', '男', '女'],
      levelDic: ['普通用户', 'VIP用户', '高级VIP用户'],
      userLevel: ['普通會員', '经销商', '副总监','总监', '区域总监', '亚洲总监','亚太区总监', '环球总监'],
      statusDic: ['申请中', '申请人撤销', '审核通过','审核驳回'],
      AcceptDic: ['不接受','接受'],
      userDialogVisible: false,
      userUpPosition:{
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    downTable(url){
      window.open(url)
    },
    getList() {
      this.listLoading = true
      if(this.listQuery.id){
        userUpPosition(this.listQuery.id).then(response => {
          console.log(response)
          this.list = [];
          if(response.data.data){
            this.list.push(response.data.data)
            this.total = 1
            this.listLoading = false
          }else{
            this.list = []
            this.total = 0
            this.listLoading = false
          }
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      }else{
        fetchList(this.listQuery).then(response => {
          console.log(response)
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      }
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['用户名', '手机号码', '性别', '生日', '状态']
        const filterVal = ['username', 'mobile', 'gender', 'birthday', 'status']
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '用户信息')
        this.downloadLoading = false
      })
    },
    handleDetail(row,flag) {
      this.isdisabled=flag
      this.userDetail = row
      this.uStatus = row.ustatus
      this.reviewDesc = row.reviewDesc
      this.userDialogVisible = true
      this.id = row.id
    },
    handleUserUpdate(){
     updateUser({id:this.id,stat:this.uStatus,reviewDesc:this.reviewDesc})
        .then((response) => {
          this.userDialogVisible = false
          this.id = ''
          this.uStatus = ''
          this.reviewDesc = ''
          this.getList()
          this.$notify.success({
            title: '成功',
            message: '更新用户成功'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    }
  }
}
</script>
